import 'dart:convert';

import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:encrypt/encrypt.dart' as Encrypt;
import 'package:encrypt/encrypt.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/11/27.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
/// 加密处理工具类  EncryptUtils 类名调用
/// lib/utils/encrypt_utils.dart
class EncryptUtils {
  /// md5 加密
  static String encodeMd5(String data) {
    var content = Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    return hex.encode(digest.bytes);
  }

  /// Base64 编码 加密
  static String encodeBase64(String data) {
    var content = utf8.encode(data);
    var digest = base64Encode(content);
    return digest;
  }

  /// Base64 编码 解密
  static String decodeBase64(String data) {
    List<int> bytes = base64Decode(data);
    String result = utf8.decode(bytes);
    return result;
  }
}

void main() {
  String value =
      "ngFWizPS39870Eg1HW2V8RchzeXzr6KxCl471l8tqEsXhUQopfqhsNw2XWR8mAHBBA3GDcHM4IPBZqDqht5Vp/CE01asqcGGn00jNw1qGzPQ5lfkpg1iKndIUG+PCd40sD5s06DiMVhEJ5nDbOtyM2YBLVGMYlF4AzzpIEx1RQJBZKfcuAXP7NGnSh4OrdI3OBApz3HHewPo0e/+TO+yMIImywq/koto60Z1goKfwyUSsCTCl2Reb/osp1EZ25JitKrJFc4zjKx2tndhzDMMpeZMrkTE4IMbFhCEOk1bfyCT4ASl1BuITYOdOpQBYv1HAl6z8lc385MgPAmiAVRNuftsw9RZ+XMikMGQldKelUJzi8LDUV8yJKmbpn9AzfSwoQKNA3cCLH/u+8zBRry6VjkrTa4zemp1UGJ8Ck8dVHq7mzJ4STV1YOSAeaWvtOXRER/AuUrVu8L0gxPJ9/CBvYGz4QPjoHn7JYPOUhPkJJxHhDwphZbgkOfavsZeaJoRU0ZTqUP4aNXzLcZH6dDPZjtemMm/6Lfd3x0n0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qV6u1WnBp6ewqYZvKXLfpN2Ckbile2CYOy1BevlPRQTjNGC3qJk0qjtN3spjvn6fDTlDo7i0CgU5/qZXj3WFkCFcSsbWzoSBC8HPM699DnuvQ57o/KqrhrSUdlJoPJNY2qeP/PYnWEK5gWvNPwMf1aRGCYxnyW0hM4IUuNjLKxZtmDipk4xPNqEK62tXZBktZhlaPZp3lat4BusE4igUXcM9bBiXY4+fn9b9YaYAhwFsBD9UGJndWCTQJi38/224eZsKQ1yuOeDp0O8CgHkl0K6AF2JytSfqI2jnsz4lWFpDG5OcHIkU7ryAn2rTvVN8cFcT8fu/oXCRWiZu1fsRPjjHmsxUzjZ+67S8GK4P/XUnkPKfRLAvOABPL0YDUt3n+wrfRttrn4qK38BcEIiNCm3pSZA9gupK4c7c6KtuGfLqkvlr60lXk/ASBth1Dk2OLKFe6ccx5cdfIo/pmfYRSWRKqp3BPUrkmKMvo1piJPlHyBKi38lo1ETVWB4Ej2mmX0O2wI5C3MuSwBRcIb+gjeE5j3QyZWTYIQbQz9o6U7p7MvBNzxbEyY9CXoao2OJIxxHX4PF65Y8OhwFuyFLNAt05rSZ4cNDCq2zKdNsW1CKxoYWCzCt2wkpVmE1qGvI0ZeIp1qNJpgLKkotlX27emMMtezQjI+ieDNQqu5a1laPhKRrDLtVSN5BIiLY1YDP9dT/j7tpMucaai6H2FC4qBYHGNeYVZ5DqwmUJZasV6i4Bk+RvXhKi58zcGBaFGjHAtrd8fBMumBeALKjgM7fA7UUZklmRWoPvITAByC2LiAIkRBAeEUHAEG19rswP6GRDh12pVMb4VRS0c41ybSMP+zeTyxJr6WurRrIsio+TlJI5llC";
  // 明文数据和密钥
  final plainText = '{"auPhone":"10008376857","auPassword":"123456"}';
  final plainKey = 'eELB8HbcgKWkndH0g9qRcBMZG5zgPkMc';

  // 初始化加密工具
  final key = Key.fromUtf8(plainKey);
  final iv = IV.fromUtf8(plainKey.substring(0, 16));
  final encrypter = Encrypter(AES(key, mode: AESMode.cbc));

  // dart 端加密
  final encrypted = encrypter.encrypt(plainText, iv: iv);
  print(encrypted.base64);

  // 解密 java 端密文
  final javaEncryptedText =
      'BLo1Q73NNBWMxpqBF66sKrI+PYfikhD5YU2O0A+tkBRuiy1FkPxSnxUHw5rV2QZK';
  final decrypted = encrypter.decrypt64(javaEncryptedText, iv: iv);

  print(decrypted);
  final decrypted2 = encrypter.decrypt64("jUsJnjZ90PmYh7aTI5JpdQ==", iv: iv);
  print(decrypted2);
}
